package com.example.interviewrecorder.view

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.example.interviewrecorder.LyyApplicaiton
import com.example.interviewrecorder.LyyBaseActivity
import com.example.interviewrecorder.LyyDatabase
import com.example.interviewrecorder.R
import com.example.interviewrecorder.components.ChangePasswordDialog
import com.example.interviewrecorder.components.showTip
import com.example.interviewrecorder.dao.LyyUserDao
import com.example.interviewrecorder.databinding.ActivitySecurityCenterBinding
import com.example.interviewrecorder.utils.LyyActivityCollector
import com.example.interviewrecorder.utils.LyyLogUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

private const val TAG = "SecurityCenterActivity"

class SecurityCenterActivity : LyyBaseActivity(), View.OnClickListener {
    private val binding: ActivitySecurityCenterBinding by lazy {
        ActivitySecurityCenterBinding.inflate(layoutInflater)
    }
    private val lyyUserDao: LyyUserDao = LyyDatabase.getDatabase().getLyyUserDao()
    private val changePasswordDlg: ChangePasswordDialog by lazy {
        ChangePasswordDialog(
            context = this,
            onConfirm = { oldPassword, newPassword1, newPassword2, dlg ->
                LyyLogUtil.d(TAG, "点击了修改密码弹框中的确定按钮")
                val currUser = LyyApplicaiton.currUser
                val currPassword = currUser?.password
                if (oldPassword != currPassword) {
                    showTip(content = getString(R.string.old_password_not_correct_tip))
                    return@ChangePasswordDialog
                }
                if (newPassword1 != newPassword2) {
                    showTip(content = getString(R.string.new_old_password_not_same_tip))
                    return@ChangePasswordDialog
                }
                lifecycleScope.launch {
                    launch(Dispatchers.IO) {
                        repeatOnLifecycle(Lifecycle.State.STARTED) {
                            currUser.password = newPassword1
                            runCatching {
                                lyyUserDao.updateLyyUser(currUser)
                            }.onSuccess {
                                runOnUiThread {
                                    showTip(content = getString(R.string.change_password_success_tip), onPositiveBtnClick = {
                                        LyyApplicaiton.currUser = null
                                        LyyActivityCollector.finishAll()
                                    })
                                }
                            }.onFailure {
                                runOnUiThread {
                                    showTip(content = getString(R.string.change_password_fail_tip))
                                }
                            }
                        }
                    }
                }
            }

        )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        initEvents()
    }

    private fun initEvents() {
        binding.changePasswordLl.setOnClickListener(this)
        binding.securityQuestionLl.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.change_password_ll -> {
                changePasswordDlg.show()
            }
            R.id.security_question_ll->{
                SecurityQuestionActivity.start()
            }
        }
    }

    companion object {
        fun start() {
            val currAct = LyyActivityCollector.getCurrActivity()
            val intent = Intent(currAct, SecurityCenterActivity::class.java)
            currAct.startActivity(intent)
        }
    }
}